package com.imooc.sell.utils;

import lombok.Getter;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

/**
 * 自定义id策略生成器
 */
@Getter
public class MyGenerator implements IdentifierGenerator {
    /**
     * 改良uuid
     */
    public MyGenerator() {
    }

    @Override
    public Serializable generate(SharedSessionContractImplementor s, Object o) throws HibernateException {
        return genId();
    }

    private Serializable genId() {
        String id;
        int machineId = 1;
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (0 > hashCodeV)
            hashCodeV = -hashCodeV;
        id = String.valueOf(machineId + hashCodeV);
        return id;
    }
}
